<?php
namespace app\admin\controller;

use app\admin\model\DocManage as DocManageModel;
use think\Db;
use think\Request;


class DocManage extends Base
{
    public function index()
    {
        $model = new DocManageModel;

        //渲染页面模板
        $model->field('d.id,d.text,d.type_id,d.created_at,d.updated_at,d.name as dname,d.remark,
        a.username as ausername,b.username as busername')/*,b.username as bname*/
        ->alias('d')
            ->join('admin a','d.admin_id = a.id ','left')
            ->join('admin b','d.last_admin_id = b.id','left')
            //->where('g.admin_job_id','j.id')
            ->order('d.id','desc');
       // ->select();
        //->paginate(5);

        //搜索名称
        $phname = $this->request->param("phname");

        if(!empty($phname)){
            $model->where('d.name','like', '%'.$phname.'%');
        }
        $result = $model->paginate(10,false,[
            'query'=>['phname'=>$phname]
        ]);

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $_POST["viewId"];
            $view = $model->getAll()->where("id",$id)->find();
            /*if(empty($view["editor"])){
                $view["editor"] = "";
            }*/
            return json([
                'code' => 200,
                'view'=>$view,
            ]);
        }

        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('docmanage',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }

    public function create()
    {
        //接收ajax传递的值
        if($this->request->isPost()){
            $formdata = $this->request->post();
            
            $formdata['text'] = $formdata['text' . $formdata['type_id']];
            unset($formdata['text0']);
            unset($formdata['text1']);
            unset($formdata['text2']);

            $username = Db::query("select `id`,`name` from ". $this->dbTablePrefix ."doc_manage where `name`='".$formdata['name']."' and deleted_at is NULL ");

            if(!empty($username)){
                return json([
                    'code' => 4000,
                    'message'=>"用户名已存在"
                ]);
            }
            $model = new DocManageModel;

            $result = $model->allowField(true)->save($formdata);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    public function edit($id)
    {
        $model = new DocManageModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();

        if($ispost){
            $formdata = $this->request->post();
            if($formdata["type_id"]==1){
                $formdata["text"] = $formdata["text1"];
            }elseif ($formdata["type_id"]==0){
                $formdata["text"] = $formdata["text0"];
            }elseif ($formdata["type_id"]==2){
                $formdata["text"] = $formdata["text2"];
            }

            $result = $model->allowField(true)->save($formdata,['id'=>$id]);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //删除单条记录
    public function delete($id)
    {
        $result = DocManageModel::update(['deleted_at'=>date('Y-m-d H:i:s')],['id'=>$id]);//软删除

        if($result){
            return json([
                'code' => 200,
            ]);
        }

    }

    //批量删除
    public function deleteArr()
    {
        $ispost = $this->request->isPost();
        if ($ispost) {
            $idArr = $this->request->post();

            $result = DocManageModel::where('id','exp','in('.implode(',',$idArr['id']).')')->update(['deleted_at'=>date('Y-m-d H:i:s')]);//软删除

            if($result){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }
}